<template>
  <div class="task-list">
    <el-card class="box-card">
      <div slot="header" class="header">
        <span class="title">我的任务列表</span>
        <el-button type="primary" @click="showAddTaskModal" class="add-btn">添加任务</el-button>
      </div>
      <el-table :data="tasks" class="task-table">
        <el-table-column prop="name" label="任务名称" width="180"></el-table-column>
        <el-table-column prop="completed" label="完成状态" width="100">
          <template slot-scope="scope">
            <el-tag :type="scope.row.completed ? 'success' : 'info'">
              {{ scope.row.completed ? '已完成' : '未完成' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button size="mini" @click="editTask(scope.row)" class="edit-btn">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      tasks: []
    };
  },
  created() {
    this.fetchTasks();
  },
  methods: {
    fetchTasks() {
      const token = localStorage.getItem('token');
      axios.get('/api/tasks/mytasks', {
        headers: {
          'Authorization': token
        }
      })
      .then(response => {
        this.tasks = response.data.data;
      })
      .catch(error => {
        this.$message.error('获取任务列表失败');
      });
    },
    showAddTaskModal() {
      // 使用 Vue Router 的编程式导航跳转到添加任务页面
      this.$router.push('/addtodo');
    },
    editTask(task) {
      this.$router.push({
        name: 'edit-task',
        query: { taskId: task.id }
      });
    }
  }
};
</script>

<style scoped>
.task-list {
  padding: 20px;
}

.box-card {
  border-radius: 10px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 18px;
  font-weight: bold;
}

.add-btn {
  margin-left: 20px;
}

.task-table {
  margin-top: 20px;
}

.edit-btn {
  color: #409EFF;
}
</style>
